package main

import (
	chat "chat/config"
	log2 "chat/log"
	"chat/service"
	"context"
	"flag"
	"fmt"
	"log"
	"net"

	"google.golang.org/grpc"
)

var chatAddr = flag.Int("chatAddr", 16012, "the address to connect to")

func main() {
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf("chat:%d", *chatAddr))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chat", "GRPC server", "chat GRPC server", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	chat.RegisterProdChatSystemServiceServer(s, &service.ChatSystem{})
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "chat", "GRPC server", "chat GRPC server", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		log.Fatalf("failed to serve: %v", err)
	}
}
